package com.sphd.emanu.fragment.aterials;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.sphd.emanu.R;

import com.sphd.emanu.activity.materialsstorelist.MaterialsStoreListActivity;

import com.sphd.emanu.adapter.TMtCategoryAdapter;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.mvp.MVPBaseFragment;
import com.sphd.emanu.permission.Moudle;
import com.sphd.emanu.pojo.goods.Category;
import com.sphd.emanu.pojo.login.User;
import com.sphd.emanu.utils.PermissionConfig;
import com.wzutils.L;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * 库房------材料库存页面
 *
 * @Author Hu
 * @Time 2017/5/25 -- 15:55.
 * @Email 87683202@qq.com
 */

public class AterialsFragment extends MVPBaseFragment<AterialsContract.View, AterialsPresenter> implements AterialsContract.View {
    Bundle bundle;
    User userEntity;
    int level = 0;
    @BindView(R.id.my_recycler)
    RecyclerView myRecycler;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    Unbinder unbinder;
    TMtCategoryAdapter tMtCategoryAdapter;
    Intent intent;
    boolean isCanDo = false;
    List<Moudle> moudleList;

    @Override
    protected int getLayoutId() {
        return R.layout.myswf_and_recycler;
    }

    @Override
    protected void initView() {
        unbinder = ButterKnife.bind(this, rootView);
        intent = new Intent();
        initEvent();
        bundle = getArguments();
        userEntity = (User) bundle.getSerializable(Constant.USER_ENTITY);
        moudleList = (List<Moudle>) bundle.getSerializable(Constant.PERMISSION_LIST);
        isCanDo = isCanDo(moudleList, PermissionConfig.MENU_STOREHOUSE_8);
        setRecyWithDecoration(myRecycler);
        initAdapter();
        L.e("initView()");
        initData();
    }

    private void initEvent() {
        mySwf.setOnRefreshListener(() -> addData());
        myRecycler.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                Category category = (Category) adapter.getItem(position);
                bundle.putString(Constant.CATEGORY_PARENT_NAME, category.getName());
                bundle.putSerializable(Constant.CATEGORY, category);
                bundle.putSerializable(Constant.CATEGORY_PARENT, category);
                bundle.putInt(Constant.LEVEL,1);
                intent.setClass(context, MaterialsStoreListActivity.class);
//                intent.setClass(context, SecondLevelActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }

            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                super.onItemChildClick(adapter, view, position);
                final Category category = (Category) adapter.getItem(position);
                View viewDialog = View.inflate(context, R.layout.dialog_edit, null);
                final MaterialEditText myEdit = (MaterialEditText) viewDialog.findViewById(R.id.my_edit);
                myEdit.setText(category.getName());
                final AlertDialog alertDialog = getDialogBuilder(viewDialog, "编辑分类", "确定", "取消");
                alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (TextUtils.isEmpty(myEdit.getText().toString().trim())) {
                            toast("分类不能为空");
                        } else {
                            mPresenter.updateMaterialCategory(category.getId(), 1, myEdit.getText().toString().trim(), userEntity.getOid(), alertDialog);
                        }
                    }
                });
            }

            @Override
            public void onItemLongClick(BaseQuickAdapter adapter, View view, final int position) {
                super.onItemLongClick(adapter, view, position);
                final Category category = (Category) adapter.getItem(position);
                if (isCanDo) {
                    if (!category.getIsLocked()) {
                        new SweetAlertDialog(context, SweetAlertDialog.WARNING_TYPE).setTitleText("确定要删除此分类吗？").setConfirmText("确定")
                                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                                    @Override
                                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                                        mPresenter.deleteMaterialCategory(category.getId(), position, sweetAlertDialog);
                                    }
                                }).setCancelText("取消").setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                            @Override
                            public void onClick(SweetAlertDialog sweetAlertDialog) {
                                sweetAlertDialog.dismiss();
                            }
                        }).show();
                    }

                }
            }
        });
    }

    private void initAdapter() {
        setRefresh(mySwf);
        tMtCategoryAdapter = new TMtCategoryAdapter(R.layout.item_tmt_category, new ArrayList<Category>(), isCanDo);
        if (null != tMtCategoryAdapter.getEmptyView()) {
            tMtCategoryAdapter.setEmptyView(emptyView);
        }
        myRecycler.setAdapter(tMtCategoryAdapter);
    }

    @Override
    protected void initData() {
        L.e("initData()");
        addData();
    }

    private void addData() {
        showProgressDialog();
        mPresenter.selectFristCategory(userEntity.getOid());
    }

    @Override
    public void setList(List<Category> categories) {
        dissProgressDialog();
        tMtCategoryAdapter.setNewData(categories);
    }

    @Override
    public void setDelRes(int res, int pos, SweetAlertDialog sweetAlertDialog) {
        if (res == 1) {
            toast("删除成功");
            tMtCategoryAdapter.remove(pos);
            sweetAlertDialog.dismiss();
        } else {
            toast("删除失败，请重试！");
        }
    }

    @Override
    public void setAddRes(int res, AlertDialog viewDialog) {
        if (res == 1) {
            toast("操作成功");
            viewDialog.dismiss();
        } else {
            toast("操作失败，请重试。");
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

}
